<template>
  <div>
    <div class="container" v-if="detailData">
      <!-- 客户信息卡片 -->
      <main class="main-content">
        <div class="customer-card">
          <div class="card-header">
            <div class="avatar-section">
              <div class="avatar">{{
                detailData.sourceName ? detailData.sourceName[0] : '未'
              }}</div>
              <div class="online-status"></div>
            </div>
            <div class="customer-info">
              <h2 id="customerName"
                >{{ detailData.sourceName || '未命名' }} {{ detailData.callee }}</h2
              >
              <div class="customer-meta">
                <span class="customer-type high-intent">{{
                  detailData.levelName || '无等级'
                }}</span>
                <span class="join-time">{{ formatDate(detailData.createTime) }}</span>
              </div>
            </div>
            <div class="quick-actions">
              <button class="action-btn call-btn" @click="tel">📞</button>
              <button class="action-btn message-btn" @click="sendText">💬</button>
            </div>
          </div>

          <div class="card-body">
            <!-- AI销售建议区域 -->
            <div class="ai-sales-suggestions">
              <div class="suggestions-header">
                <h3 style="margin: 0">AI销售建议</h3>
                <span class="suggestions-badge">智能分析</span>
              </div>
              <div class="suggestions-grid">
                <div
                  class="suggestion-item"
                  :class="modelClass[index % 4]"
                  v-for="(items, index) in detailData.summary"
                  :key="index"
                >
                  <div class="suggestion-content">
                    <div class="suggestion-title">{{ items.name }}</div>
                    <div class="suggestion-desc">{{ items.value }}</div>
                  </div>
                </div>
              </div>
            </div>

            <div class="service-section">
              <button class="service-btn secondary" @click="scrollToChat">
                <span>滚动到对话</span>
              </button>
              <button class="service-btn primary" @click="playRecording" v-if="audioIsPlay">
                <span>播放录音</span>
              </button>
              <button class="service-btn primary" @click="playRecording" v-else>
                <span>暂停播放</span>
              </button>
            </div>
          </div>
        </div>

        <!-- AI员工对话记录区域 -->
        <div class="chat-section" id="chatSection" ref="chatRef">
          <div class="section-header">
            <h3>AI-Agent对话记录</h3>
          </div>
          <div class="chat-container" id="chatContainer">
            <div v-for="item in nlpResult" :key="item.id">
              <div class="chat-message customer-left" v-if="item.type == 1">
                <div class="message-avatar">客</div>
                <div class="message-content">
                  <div class="message-text">{{ item.result }}</div>
                  <div class="message-time">{{ formatDate(item.createTime) }}</div>
                  <div class="message-source">来源：机器人外呼</div>
                </div>
              </div>

              <div class="chat-message ai-agent-right" v-if="item.type == 2">
                <div class="message-avatar">AI</div>
                <div class="message-content">
                  <div class="ai-tag">AI员工</div>
                  <div class="message-text">{{ item.result }}</div>
                  <div class="message-time">{{ formatDate(item.createTime) }}</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </main>
    </div>
    <audio controls ref="audioRef" v-if="detailData" style="display: none">
      <source :src="detailData.recordUrl" type="audio/mpeg" />
      您的浏览器不支持音频播放
    </audio>
  </div>
</template>

<script setup>
import axios from 'axios'
import O_user from '@/assets/imgs/o_user.png'
import O_Kf from '@/assets/imgs/o_kf.png'
import { formatDate } from '@/utils/formatTime'

const route = useRoute()
const uniqueId = ref(null)
const audioRef = ref(null)
const audioIsPlay = ref(true)

const modelClass = {
  0: 'priority-high',
  1: 'strategy',
  2: 'timing',
  3: 'offer'
}
/**
 * 播放录音
 * */
const playRecording = () => {
  if (audioRef.value.paused) {
    audioRef.value.play()
    audioIsPlay.value = false
  } else {
    audioRef.value.pause()
    audioIsPlay.value = true
  }
}
const detailData = ref(null)
/**
 * 获取通话记录详情
 * */
const getRecord = (code) => {
  axios
    .get(import.meta.env.VITE_BASE_URL + `/open-api/call/record/get?reqNo=${uniqueId.value}`)
    .then((res) => {
      if (res.data.code == 0) {
        const data = res.data.data
        if (data.summary) {
          const obj = JSON.parse(data.summary)
          const res = Object.entries(obj).map(([key, value]) => ({
            name: key,
            value: value
          }))
          data.summary = res
        }
        detailData.value = data
      }
    })
}
const nlpResult = ref([])
/**
 * 获取对话记录详情
 * */
const getNlpResult = (code) => {
  axios
    .get(
      import.meta.env.VITE_BASE_URL + `/open-api/call/record/nlp-result?callId=${uniqueId.value}`
    )
    .then((res) => {
      if (res.data.code == 0) {
        nlpResult.value = res.data.data
      }
    })
}
/**
 * 拨打电话
 * */
const tel = () => {
  if (detailData.value) {
    window.location.href = `tel:${detailData.value.callee}`
  }
}
/**
 * 发送短信
 * */
const sendText = () => {
  if (detailData.value) {
    window.location.href = `sms:${detailData.value.callee}`
  }
}
/**
 * 滚动到指定元素的位置
 * */
const chatRef = ref(null)
const scrollToChat = () => {
  const container = document.querySelector('.main-content')
  const target = document.getElementById('chatSection')

  if (container && target) {
    // 添加20px的上边距
    const scrollPosition = target.offsetTop - container.offsetTop - 20
    container.scrollTo({
      top: Math.max(0, scrollPosition), // 确保不小于0
      behavior: 'smooth'
    })
  }
}
onMounted(() => {
  uniqueId.value = route.query.uniqueId
  if (uniqueId.value) {
    getRecord()
    getNlpResult()
  } else {
    ElMessage.warning('请检查参数')
  }
  document.title = '新客户跟进通知'
})
</script>

<style lang="scss" scoped>
#customerName {
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
    sans-serif;
  background: #f5f7fa;
  min-height: 100vh;
  color: #2c3e50;
  overflow-x: hidden;
}

.container {
  max-width: 428px;
  margin: 0 auto;
  background: #fff;
  min-height: 100vh;
  position: relative;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.05);
}

/* 头部样式 */
.header {
  background: #fff;
  color: #2c3e50;
  padding: 20px;
  position: sticky;
  top: 0;
  z-index: 100;
  border-bottom: 1px solid #e8ecf0;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header h1 {
  font-size: 20px;
  font-weight: 600;
  color: #2c3e50;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.search-btn,
.settings-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  width: 36px;
  height: 36px;
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 16px;
}

.search-btn:hover,
.settings-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.05);
}

.search-bar {
  display: flex;
  gap: 10px;
  max-height: 0;
  overflow: hidden;
  transition: all 0.3s ease;
  margin-bottom: 15px;
}

.search-bar.active {
  max-height: 50px;
}

.search-bar input {
  flex: 1;
  padding: 12px 16px;
  border: none;
  border-radius: 25px;
  background: rgba(255, 255, 255, 0.9);
  font-size: 14px;
  outline: none;
}

.search-bar button {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  width: 40px;
  height: 40px;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 16px;
}

/* 主内容 */
.main-content {
  padding: 20px;
  background: #f8f9fa;
  height: calc(100vh - 40px);
  overflow-y: auto;
  position: relative;
}

/* 客户信息卡片 */
.customer-card {
  height: calc(100% - 2px);
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  overflow: auto;
  margin-bottom: 20px;
  transition: all 0.2s ease;
  border: 1px solid #e8ecf0;
}

.card-header {
  padding: 10px 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  background: #fff;
  border-bottom: 1px solid #f0f2f5;
}

.avatar-section {
  position: relative;
}

.avatar {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: #667eea;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  font-weight: 600;
}

.online-status {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #52c41a;
  border: 2px solid white;
}

.customer-info {
  flex: 1;
}

.customer-info h2 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 8px;
  color: #2c3e50;
  line-height: 1.3;
}

.customer-meta {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* 客户类型样式 */
.customer-type {
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 12px;
  font-weight: 500;
  display: inline-block;
  width: fit-content;
}

.customer-type.high-intent {
  background: #fff2e8;
  color: #fa8c16;
  border: 1px solid #ffd591;
}

.customer-type.medium-intent {
  background: #fff7e6;
  color: #faad14;
  border: 1px solid #ffe58f;
}

.customer-type.a-level {
  background: #fff1f0;
  color: #f5222d;
  border: 1px solid #ffa39e;
}

.customer-type.b-level {
  background: #fff2e8;
  color: #fa541c;
  border: 1px solid #ffbb96;
}

.customer-type.c-level {
  background: #f6ffed;
  color: #52c41a;
  border: 1px solid #b7eb8f;
}

.join-time {
  color: #8c8c8c;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.quick-actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  padding: 8px 16px;
  border-radius: 6px;
  border: 1px solid #d9d9d9;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  font-size: 12px;
  background: #fff;
  color: #595959;
}

.call-btn:hover {
  border-color: #52c41a;
  color: #52c41a;
}

.message-btn:hover {
  border-color: #1890ff;
  color: #1890ff;
}

/* 卡片主体 */
.card-body {
  padding: 20px;
}

/* AI销售建议区域 */
.ai-sales-suggestions {
  margin-bottom: 24px;
}

.suggestions-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.suggestions-header h3 {
  font-size: 16px;
  color: #2c3e50;
  font-weight: 600;
}

.suggestions-badge {
  background: #f0f2ff;
  color: #667eea;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 500;
  border: 1px solid #d6e4ff;
}

.suggestions-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.suggestion-item {
  background: #fafafa;
  border-radius: 8px;
  padding: 16px;
  transition: all 0.2s ease;
  border: 1px solid #f0f0f0;
}

.suggestion-item:hover {
  background: #fff;
  border-color: #d9d9d9;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.priority-high {
  border-left: 3px solid #fa541c;
}

.strategy {
  border-left: 3px solid #1890ff;
}

.timing {
  border-left: 3px solid #faad14;
}

.offer {
  border-left: 3px solid #52c41a;
}

.suggestion-content {
  flex: 1;
}

.suggestion-title {
  font-size: 13px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 6px;
}

.suggestion-desc {
  font-size: 12px;
  color: #8c8c8c;
  line-height: 1.4;
}

/* 服务按钮 */
.service-section {
  display: flex;
  gap: 12px;
}

.service-btn {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s ease;
  background: #fff;
}

.service-btn.primary {
  background: #1890ff;
  color: white;
  border-color: #1890ff;
}

.service-btn.primary:hover {
  background: #40a9ff;
  border-color: #40a9ff;
}

.service-btn.secondary {
  background: #fff;
  color: #595959;
  border-color: #d9d9d9;
}

.service-btn.secondary:hover {
  border-color: #40a9ff;
  color: #40a9ff;
}

/* 聊天区域 */
.chat-section {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  overflow: hidden;
  border: 1px solid #e8ecf0;
  position: sticky;
  top: 0px;
  height: 100vh;
}

.section-header {
  padding: 0 20px;
  background: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #f0f2f5;
}

.section-header h3 {
  font-size: 16px;
  color: #2c3e50;
  font-weight: 600;
}

.chat-controls {
  display: flex;
  gap: 8px;
}

.filter-btn {
  padding: 6px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s ease;
  background: #fff;
  color: #595959;
}

.filter-btn:hover {
  border-color: #40a9ff;
  color: #40a9ff;
}

/* 筛选面板 */
.filter-panel {
  padding: 0 20px;
  max-height: 0;
  overflow: hidden;
  transition: all 0.3s ease;
  background: #fafafa;
}

.filter-panel.active {
  padding: 16px 20px;
  max-height: 100px;
}

.filter-options {
  display: flex;
  gap: 12px;
}

.filter-options select {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  background: white;
  outline: none;
  cursor: pointer;
  font-size: 12px;
}

/* 聊天消息 - 左右对话布局 */
.chat-container {
  padding: 20px;
  height: calc(100vh - 94px);
  overflow-y: auto;
}

.chat-message {
  display: flex;
  margin-bottom: 16px;
  max-width: 85%;
}

/* 客户消息 - 左侧 */
.chat-message.customer-left {
  align-self: flex-start;
  gap: 12px;
}

.chat-message.customer-left .message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #52c41a;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  flex-shrink: 0;
}

.chat-message.customer-left .message-content {
  background: #f0f2f5;
  padding: 12px 16px;
  border-radius: 12px;
  border-top-left-radius: 4px;
  position: relative;
}

/* AI员工消息 - 右侧 */
.chat-message.ai-agent-right {
  align-self: flex-end;
  margin-left: auto;
  gap: 12px;
  flex-direction: row-reverse;
}

.chat-message.ai-agent-right .message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #1890ff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  flex-shrink: 0;
}

.chat-message.ai-agent-right .message-content {
  background: #e6f7ff;
  padding: 12px 16px;
  border-radius: 12px;
  border-top-right-radius: 4px;
  position: relative;
  text-align: right;
}

.message-text {
  color: #2c3e50;
  line-height: 1.5;
  margin-bottom: 6px;
  font-size: 14px;
}

.message-time {
  font-size: 11px;
  color: #8c8c8c;
}

.message-source {
  font-size: 10px;
  color: #bfbfbf;
  margin-top: 4px;
  font-style: italic;
}

.message-status {
  font-size: 11px;
  color: #52c41a;
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 4px;
  justify-content: flex-end;
}

.ai-tag {
  background: #1890ff;
  color: white;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 10px;
  margin-bottom: 6px;
  display: inline-block;
  font-weight: 500;
}

/* 快速回复 */
.quick-reply-section {
  padding: 20px;
  border-top: 1px solid #f0f2f5;
  background: #fafafa;
}

.quick-reply-section h4 {
  font-size: 14px;
  color: #595959;
  margin-bottom: 12px;
  font-weight: 600;
}

.quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.quick-reply-btn {
  padding: 8px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 16px;
  background: white;
  color: #595959;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s ease;
}

.quick-reply-btn:hover {
  background: #1890ff;
  color: white;
  border-color: #1890ff;
}

/* 加载指示器 */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.loading-overlay.active {
  display: flex;
}

.loading-spinner {
  text-align: center;
  color: white;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top: 3px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 12px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .container {
    max-width: 100%;
  }
  .suggestions-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .service-section {
    flex-direction: column;
  }

  .customer-meta {
    flex-direction: column;
    gap: 4px;
  }

  .chat-message {
    max-width: 90%;
  }
}

/* 滚动条样式 */
.chat-container::-webkit-scrollbar {
  width: 4px;
}

.chat-container::-webkit-scrollbar-track {
  background: #f5f5f5;
  border-radius: 2px;
}

.chat-container::-webkit-scrollbar-thumb {
  background: #d9d9d9;
  border-radius: 2px;
}

.chat-container::-webkit-scrollbar-thumb:hover {
  background: #bfbfbf;
}

/* 增强的视觉效果 */
.customer-card:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.chat-section:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}
</style>
